package com.hsnn.medstgmini.pro.base.controller;

import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.service.DicAreaManager;
import com.hsnn.medstgmini.base.std.model.ViewDrug;
import com.hsnn.medstgmini.model.FilingPolicy;
import com.hsnn.medstgmini.pro.base.model.BaseBakpurchase;
import com.hsnn.medstgmini.pro.base.model.ViewBakpurchase;
import com.hsnn.medstgmini.pro.base.service.BakpurchaseManager;
import com.hsnn.medstgmini.pro.base.service.BaseBakpurchaseManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.Pagination;


@Controller
@RequestMapping("/bakpurchase")
public class BakpurchaseController {

	private static final Logger log = Logger.getLogger(BakpurchaseController.class);
	
	@Autowired
	private BakpurchaseManager bakpurchaseManager;
	
	@Autowired
	private BaseBakpurchaseManager baseBakpurchaseManager;

	@Autowired
	private Properties bakPurchaseProp;
	
	@Autowired
	private  HttpServletRequest request;

	@Autowired
	private DicAreaManager dicAreaManager;
	
    @Value("#{sysProp['system.province']}")
    private String sysProvince;
    
    @Autowired
    private FilingPolicy policy;
    
	/**
	 * @category 进入备案药品政策查看页面
	 * @auth 陈镜宇
	 * @param model
	 * @param bidProject
	 * @return
	 * @date 2015年6月15日
	 */
	@RequestMapping("/toAdd")
	public String toAddPage(Model model) {
		model.addAttribute("policy", policy);
		return "/pro/bak/bakDrugDoc";
	}
	/**
	 * @category 进入备案药品申请列表页面
	 * @auth 单大伟
	 * @param model
	 * @param bidProject
	 * @return
	 * @date 2015年6月15日
	 */
	@RequestMapping("/toAddList")
	public String toAddList(Model model) {
		return "/pro/bak/bakDrugAddList";
	}
	/**
	 * @category 获取待备案药品列表
	 * @auth 陈镜宇
	 * @param model
	 * @param bidProject
	 * @return
	 * @date 2015年6月15日
	 */
	@RequestMapping("/getAddList")
	@ResponseBody
	public Pagination getAddList(ViewDrug drug) {
		Pagination page=new Pagination(request);
		if (StringUtils.isBlank(drug.getDrugCode()) 
				&& StringUtils.isBlank(drug.getDrugName()) 
				&& StringUtils.isBlank(drug.getCompCode())) {
			return page;
		}
		page.getConditions().put("drugCode", drug.getDrugCode());
		page.getConditions().put("drugName", drug.getDrugName());
		page.getConditions().put("compCode", drug.getCompCode());
		return bakpurchaseManager.getAddList(page);
	}
	
	/**
	 * @category 进入备案药品申请页面
	 * @auth 陈镜宇
	 * @param model
	 * @param bidProject
	 * @return
	 * @date 2015年6月15日
	 */
	@RequestMapping("/toBakDrugAdd")
	public String toBakDrugAdd(Model model,String drugCode) {
		model.addAttribute("drug", bakpurchaseManager.getDrugInfo(drugCode));
		model.addAttribute("policy", policy);
		return "/pro/bak/bakDrugAdd";
	}
	
	/**
	 * @category 获取配送企业列表
	 * @auth 陈镜宇
	 * @param model
	 * @param bidProject
	 * @return
	 * @date 2015年6月15日
	 */
	@RequestMapping("/getCompList")
	@ResponseBody
	public Pagination getCompList(String compCode,String compName,String compType) {
		Pagination page=new Pagination(request);
		page.getConditions().put("compCode", compCode);
		page.getConditions().put("compName", compName);
		page.getConditions().put("compType", compType);
		return bakpurchaseManager.getCompList(page);
	}
	/**
	 * @category 提交备案申请
	 * @auth 陈镜宇
	 * @return
	 * @date 2015年6月25日
	 */
	@RequestMapping("/submitFiling")
	@ResponseBody
	public Pagination submitFiling(BaseBakpurchase bak) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO); 
		try {
			bakpurchaseManager.submitFiling(bak, user);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("exception", e);
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * @category 进入备案审批列表页面
	 * @auth 陈镜宇
	 * @param model
	 * @param bidProject
	 * @return
	 * @date 2015年6月15日
	 */
	@RequestMapping("/check")
	public String check(Model model) {
		return "/pro/bak/checkList";
	}
	/**
	 * @category 进入备案审批页面
	 * @auth 陈镜宇
	 * @param model
	 * @param bidProject
	 * @return
	 * @date 2015年6月15日
	 */
	@RequestMapping("/bakCheck")
	public String bakCheck(Model model, String bakPurId) {
		Map<String, Object> detail = baseBakpurchaseManager.getBakDetailById(bakPurId);
		model.addAttribute("detail", detail);
		model.addAttribute("bakPurId", bakPurId);
		return "/pro/bak/bakDrugSearchWeiForm";
	}
	
	/**
	 * @category 中心复审备案申请列表
	 * @date 2015年11月20日
	 * @param bak
	 * @return
	 */
	@RequestMapping("/getReauditBakList")
	@ResponseBody
	public Pagination getReauditBakList(ViewBakpurchase bak) {
		Pagination page = new Pagination(request);
		try {
			SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
			page.setForm(bak);
			baseBakpurchaseManager.getReauditBakList(page, user);
			page.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		} catch (Error error) {
			error.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 提交备案审核
	 * @auth 陈镜宇
	 * @return
	 * @date 2015年6月25日
	 */
	@RequestMapping(value = {"/passAudit", "/notPassAudit"})
	@ResponseBody
	public Pagination auditBak(BaseBakpurchase bak) {
		Pagination page=new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO); 
		try {
			Map<String, Object> detail = baseBakpurchaseManager.getBakDetailById(bak.getBakPurId());
			if (detail != null && !detail.isEmpty()) {
				bakpurchaseManager.auditBak(bak, user, detail);
				page.setSuccess(true);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			page.setSuccess(false);
		} catch(Error error) {
			log.error(error.getMessage(), error);
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * @category 卫生局审批备案列表页面
	 * @date 2015年11月21日
	 * @param model
	 * @return
	 */
	@RequestMapping("/wsjAuditBakList")
	public String wsjAuditBakList(Model model) {
		return "/pro/bak/wsjAuditBakList";
	}
	
	/**
	 * @category 卫生局审批备案列表
	 * @date 2015年11月20日
	 * @param bak
	 * @return
	 */
	@RequestMapping("/getAuditBakList")
	@ResponseBody
	public Pagination getAuditBakList(ViewBakpurchase bak) {
		Pagination page = new Pagination(request);
		try {
			SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
			page.setForm(bak);
			baseBakpurchaseManager.getAuditBakList(page, user);
			page.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		} catch (Error error) {
			error.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 卫生局审批备案
	 * @date 2015年11月21日
	 * @param model
	 * @param bakPurId
	 * @return
	 */
	@RequestMapping("/wsjAuditBak")
	public String wsjAuditBak(Model model, String bakPurId) {
		Map<String, Object> detail = baseBakpurchaseManager.getBakDetailById(bakPurId);
		model.addAttribute("detail", detail);
		model.addAttribute("bakPurId", bakPurId);
		return "/pro/bak/wsjAuditBak";
	}
	
	/**
	 * @category 卫生局审批备案
	 * @return
	 */
	@RequestMapping(value = {"/wsjPassAudit", "/wsjNotPassAudit"})
	@ResponseBody
	public Pagination wsjAuditBak(BaseBakpurchase bak) {
		Pagination page=new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO); 
		try {
			bakpurchaseManager.wsjAuditBak(bak, user);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			page.setSuccess(false);
		} catch(Error error) {
			log.error(error.getMessage(), error);
			page.setSuccess(false);
		}
		return page;
	}
	
}